package org.coc.iot.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.coc.iot.common.page.PageProperty;
import org.coc.iot.common.utils.Utils;
import org.coc.iot.constants.Constants;
import org.coc.iot.ehcache.CacheConfiguration;
import org.coc.iot.entity.SessionUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.ehcache.EhCacheCacheManager;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by Bean on 2017/5/12.
 */
public abstract class BaseController {

    @Autowired
    public CacheConfiguration cacheConfiguration;

    public String PAGE_NUM_DEFAULT = "1";

    public String PAGE_SIZE_DEFAULT = "10";

    /**
     *
     * @param request
     * @param param
     * @return
     */
    protected String getParameter(HttpServletRequest request, String param){
        String value = Utils.trim(request.getParameter(param));
        return value;
    }

    /**
     *
     * @param request
     * @param param
     * @return
     */
    protected String[] getParameters(HttpServletRequest request, String param){
        String[] values = request.getParameterValues(param);
        return values;
    }

    /**
     *
     * @param request
     * @param key
     * @param obj
     */
    protected void setSessionObject(HttpServletRequest request, String key, Object obj){
        request.getSession().setAttribute(key, obj);
    }

    /**
     *
     * @param request
     * @return
     */
    protected String getSessionUsername(HttpServletRequest request){
        SessionUser sessionUser = (SessionUser) request.getSession().getAttribute(Constants.SESSION_USER_KEY);
        EhCacheCacheManager manager = cacheConfiguration
                .ehCacheCacheManager(cacheConfiguration.ehCacheManagerFactoryBean());
        Cache authorityCache = manager.getCache("authorityCache");
        return authorityCache.get(sessionUser.getToken(), String.class);
    }

    /**
     *
     * @param request
     * @return
     */
    protected SessionUser getSession(HttpServletRequest request) {
        return (SessionUser) request.getSession().getAttribute(Constants.SESSION_USER_KEY);
    }

    /**
     *
     * @param cooks
     * @param pp
     */
    protected void setPageInfo(Cookie[] cooks, PageProperty pp, String pageSize, String pageNo) {
        //适用于 datatables
        int pageNum = Constants.PAGE_NUM_DEFAULT;

        int pageSizeNum = Constants.PAGE_SIZE_DEFAULT; // 获取每页数据条数

        if(cooks.length != 0){
            for(int i = 0; i < cooks.length; i++){
                if(cooks[i].getName().equalsIgnoreCase("limit")) {
                    pageSizeNum = Utils.parseInt(cooks[i].getValue().toString(), Constants.PAGE_SIZE_DEFAULT);
                }
            }
        }
        if (!"".equals(pageNo)) {
            // 将字符串数字转化为int型数字,把pageNo传进去，转换为整型，默认为1
            pageNum = Utils.parseInt(pageNo, Constants.PAGE_NUM_DEFAULT);
        }
        if (!"".equals(pageSize)) {
            pageSizeNum = Utils.parseInt(pageSize, Constants.PAGE_SIZE_DEFAULT);
        }
        // 更新页码值
        pp.setNpage(pageNum);
        // 更新页面查询数量值
        pp.setNpagesize(pageSizeNum);
    }
}
